<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="visible"
    >
      <el-form
        :model="inputForm"
        ref="inputForm"
        v-loading="loading"
        :class="method === 'view' ? 'readonly' : ''"
        :disabled="method === 'view'"
        label-width="120px"
      >
        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item
              label="标题"
              prop="title"
              :rules="[
                { required: true, message: '标题不能为空', trigger: 'blur' },
              ]"
            >
              <el-input
                v-model="inputForm.title"
                placeholder="请填写标题"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="content" :rules="[]">
              <el-input
                v-model="inputForm.content"
                placeholder="请填写内容"
              ></el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item
              label="模板分类"
              prop="presumeTemplateStyle.id"
              :rules="[
                {
                  required: true,
                  message: '模板分类不能为空',
                  trigger: 'blur',
                },
              ]"
            >
              <GridSelect
                title="选择模板分类"
                labelName="title"
                labelValue="id"
                :value="inputForm.presumeTemplateStyle.id"
                :limit="1"
                @getValue="
                  (value) => {
                    inputForm.presumeTemplateStyle.id = value;
                  }
                "
                :columns="[
                  {
                    prop: 'id',
                    label: '主键',
                  },

                  {
                    prop: 'title',
                    label: '模板标题',
                  },

                  {
                    prop: 'style',
                    label: '风格',
                  },
                ]"
                :searchs="[
                  {
                    prop: 'id',
                    label: '主键',
                  },
                  {
                    prop: 'title',
                    label: '模板标题',
                  },
                  {
                    prop: 'style',
                    label: '风格',
                  },
                ]"
                dataListUrl="/p_resumetemplatestyle/pResumeTemplateStyle/list"
                entityBeanName="pResumeTemplateStyle"
                queryEntityUrl="/p_resumetemplatestyle/pResumeTemplateStyle/queryById"
              >
              </GridSelect>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item
              label="附件原件（Word）"
              prop="enclosure"
              :rules="[
                {
                  required: true,
                  message: '附件原件（Word）不能为空',
                  trigger: 'blur',
                },
              ]"
            >
              <el-upload
                ref="enclosure"
                v-if="visible"
                :action="`${this.$http.BASE_URL}/sys/file/localhost/webupload/upload`"
                :headers="{ token: $cookie.get('token') }"
                :on-preview="
                  (file, fileList) => {
                    $window.location.href =
                      (file.response && file.response.url) || file.url;
                  }
                "
                :on-success="
                  (response, file, fileList) => {
                    inputForm.enclosure = fileList
                      .map(
                        (item) =>
                          (item.response && item.response.url) || item.url
                      )
                      .join('|');
                  }
                "
                :on-remove="
                  (file, fileList) => {
                    $http
                      .post(
                        `/sys/file/webupload/deleteByUrl?url=${
                          (file.response && file.response.url) || file.url
                        }`
                      )
                      .then(({ data }) => {
                        $message.success(data.msg);
                      });
                    inputForm.enclosure = fileList
                      .map((item) => item.url)
                      .join('|');
                  }
                "
                :before-remove="
                  (file, fileList) => {
                    return $confirm(`确定移除 ${file.name}？`);
                  }
                "
                multiple
                :limit="1"
                :on-exceed="
                  (files, fileList) => {
                    $message.warning(
                      `当前限制选择 1 个文件，本次选择了 ${
                        files.length
                      } 个文件，共选择了 ${
                        files.length + fileList.length
                      } 个文件`
                    );
                  }
                "
                :file-list="enclosureArra"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">添加相关附件</div>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="附件转换模板文件"
              prop="attachmentConversion"
              :rules="[]"
            >
              <el-upload
                ref="attachmentConversion"
                v-if="visible"
                :action="`${this.$http.BASE_URL}/sys/file/localhost/webupload/upload`"
                :headers="{ token: $cookie.get('token') }"
                :on-preview="
                  (file, fileList) => {
                    $window.location.href =
                      (file.response && file.response.url) || file.url;
                  }
                "
                :on-success="
                  (response, file, fileList) => {
                    inputForm.attachmentConversion = fileList
                      .map(
                        (item) =>
                          (item.response && item.response.url) || item.url
                      )
                      .join('|');
                  }
                "
                :on-remove="
                  (file, fileList) => {
                    $http
                      .post(
                        `/sys/file/webupload/deleteByUrl?url=${
                          (file.response && file.response.url) || file.url
                        }`
                      )
                      .then(({ data }) => {
                        $message.success(data.msg);
                      });
                    inputForm.attachmentConversion = fileList
                      .map((item) => item.url)
                      .join('|');
                  }
                "
                :before-remove="
                  (file, fileList) => {
                    return $confirm(`确定移除 ${file.name}？`);
                  }
                "
                multiple
                :limit="1"
                :on-exceed="
                  (files, fileList) => {
                    $message.warning(
                      `当前限制选择 1 个文件，本次选择了 ${
                        files.length
                      } 个文件，共选择了 ${
                        files.length + fileList.length
                      } 个文件`
                    );
                  }
                "
                :file-list="attachmentConversionArra"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">添加相关附件</div>
              </el-upload>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="附件类型" prop="fileType"
                :rules="[
                  {required: true, message:'附件类型不能为空', trigger:'blur'}
                 ]">
                    <el-radio-group v-model="inputForm.fileType">
                        <el-radio v-for="item in $dictUtils.getDictList('')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
                    </el-radio-group>
           </el-form-item>
        </el-col> -->
          <!-- <el-col :span="24">
            <el-form-item label="图片附件" prop="pictureAttachment" :rules="[]">
              <el-upload
                ref="pictureAttachment"
                v-if="visible"
                list-type="picture-card"
                :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/p_resumetemplate/pResumeTemplate`"
                :headers="{ token: $cookie.get('token') }"
                :on-preview="
                  (file, fileList) => {
                    $alert(
                      `<img style='width:100%' src=' ${
                        (file.response && file.response.url) || file.url
                      }'/>`,
                      {
                        dangerouslyUseHTMLString: true,
                        showConfirmButton: false,
                        closeOnClickModal: true,
                        customClass: 'showPic',
                      }
                    );
                  }
                "
                :on-success="
                  (response, file, fileList) => {
                    inputForm.pictureAttachment = fileList
                      .map(
                        (item) =>
                          (item.response && item.response.url) || item.url
                      )
                      .join('|');
                  }
                "
                :on-remove="
                  (file, fileList) => {
                    $http
                      .post(
                        `/sys/file/webupload/deleteByUrl?url=${
                          (file.response && file.response.url) || file.url
                        }`
                      )
                      .then(({ data }) => {
                        $message.success(data.msg);
                      });
                    inputForm.pictureAttachment = fileList
                      .map((item) => item.url)
                      .join('|');
                  }
                "
                :before-remove="
                  (file, fileList) => {
                    return $confirm(`确定移除 ${file.name}？`);
                  }
                "
                multiple
                :limit="1"
                :on-exceed="
                  (files, fileList) => {
                    $message.warning(
                      `当前限制选择 1 个文件，本次选择了 ${
                        files.length
                      } 个文件，共选择了 ${
                        files.length + fileList.length
                      } 个文件`
                    );
                  }
                "
                :file-list="pictureAttachmentArra"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="是否VIP" prop="whetherVip"
                :rules="[
                 ]">
                    <el-radio-group v-model="inputForm.whetherVip">
                        <el-radio v-for="item in $dictUtils.getDictList('')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
                    </el-radio-group>
           </el-form-item>
        </el-col> -->
          <el-col :span="24">
            <el-form-item label="备注信息" prop="remarks" :rules="[]">
              <el-input
                type="textarea"
                v-model="inputForm.remarks"
                placeholder="请填写备注信息"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button
          type="primary"
          v-if="method != 'view'"
          @click="doSubmit()"
          v-noMoreClick
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import GridSelect from "@/components/gridSelect";
export default {
  data() {
    return {
      title: "",
      method: "",
      visible: false,
      loading: false,
      enclosureArra: [],
      attachmentConversionArra: [],
      pictureAttachmentArra: [],
      inputForm: {
        id: "",
        title: "",
        content: "",
        style: "",
        enclosure: "",
        attachmentConversion: "",
        fileType: "",
        pictureAttachment: "",
        whetherVip: "",
        createBy: "root",
        remarks: "",
        deleteFalg: "0",
        presumeTemplateStyle: {
          id: "",
          content: "",
          style: "",
        },
      },
    };
  },
  components: {
    GridSelect,
  },
  methods: {
    init(method, id) {
      this.method = method;
      this.inputForm.id = id;
      if (method === "add") {
        this.title = `新建简历模板`;
      } else if (method === "edit") {
        this.title = "修改简历模板";
      } else if (method === "view") {
        this.title = "查看简历模板";
      }
      this.enclosureArra = [];
      this.attachmentConversionArra = [];
      this.pictureAttachmentArra = [];
      this.visible = true;
      this.loading = false;
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields();
        if (method === "edit" || method === "view") {
          // 修改或者查看
          this.loading = true;
          this.$http({
            url: `/p_resumetemplate/pResumeTemplate/queryById?id=${this.inputForm.id}`,
            method: "get",
          }).then(({ data }) => {
            this.inputForm = this.recover(this.inputForm, data.pResumeTemplate);
            this.inputForm.enclosure.split("|").forEach((item) => {
              if (item.trim().length > 0) {
                this.enclosureArra.push({
                  name: decodeURIComponent(
                    item.substring(item.lastIndexOf("/") + 1)
                  ),
                  url: item,
                });
              }
            });
            this.inputForm.attachmentConversion.split("|").forEach((item) => {
              if (item.trim().length > 0) {
                this.attachmentConversionArra.push({
                  name: decodeURIComponent(
                    item.substring(item.lastIndexOf("/") + 1)
                  ),
                  url: item,
                });
              }
            });
            this.inputForm.pictureAttachment.split("|").forEach((item) => {
              if (item.trim().length > 0) {
                this.pictureAttachmentArra.push({
                  name: decodeURIComponent(
                    item.substring(item.lastIndexOf("/") + 1)
                  ),
                  url: item,
                });
              }
            });
            this.loading = false;
          });
        }
      });
    },
    // 表单提交
    doSubmit() {
      this.$refs["inputForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http({
            url: `/p_resumetemplate/pResumeTemplate/save`,
            method: "post",
            data: this.inputForm,
          }).then(({ data }) => {
            this.loading = false;
            if (data && data.success) {
              this.visible = false;
              this.$message.success(data.msg);
              this.$emit("refreshDataList");
            }
          });
        }
      });
    },
  },
};
</script>
